package pers.qianyu.config;


import com.github.benmanes.caffeine.cache.*;
import org.springframework.cache.*;
import org.springframework.cache.annotation.*;
import org.springframework.cache.caffeine.*;
import org.springframework.cache.support.*;
import org.springframework.context.annotation.*;

import java.util.*;
import java.util.concurrent.*;

/**
 * 缓存管理器配置类
 *
 * @author mizzle rain
 * @date 2020-10-14 9:41
 */
@Configuration
@EnableCaching
public class CaffeineCacheConfig {
    /***
     * CacheManager 实现类
     */
    @Bean("cacheManager")
    public CacheManager cacheManager() {
        SimpleCacheManager manager = new SimpleCacheManager();
        ArrayList<CaffeineCache> caches = new ArrayList<>();
        caches.add(new CaffeineCache("users-cache",
                Caffeine.newBuilder()
                        // 指定 key 下最大缓存数据量
                        .maximumSize(1000)
                        // 最后一次访问之后 120 秒过期
                        .expireAfterAccess(120, TimeUnit.SECONDS)
                        .build()));
        manager.setCaches(caches);
        return manager;
    }
}
